package com.cslg.questionnaires.service;

import com.cslg.questionnaires.entity.Question;
import com.cslg.questionnaires.mapper.QuestionMapper;
import com.cslg.questionnaires.utils.QuestionUtils;
import com.cslg.questionnaires.utils.ServiceMessageBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@Service
public class PreViewQuestionFormService {
    @Resource
    QuestionMapper questionMapper;

    public HashMap<String, Object> getPreViewQuestion(int questionid, int owerid, String userType){
        Question question = null;
        if (userType.equals("Student")){
            question = questionMapper.getQuestionById_uId_oType(questionid,owerid,userType);
        }else{
//            userType.equals("Teacher");
            question = questionMapper.getQuestionById(questionid);
        }
        if (question == null){
            return ServiceMessageBuilder.buildMessage(404,"表单不存在！",null);
        }
        Date startdate = question.getStarttime();
        Date finishdate = question.getFinishtime();
        HashMap<String, Object> questionForm = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String starttime = sdf.format(startdate);
        String finishtime = sdf.format(finishdate);
        String content_string = question.getContent();
        ArrayList content = QuestionUtils.parseQuestionContent(content_string);
        questionForm.put("id",question.getId());
        questionForm.put("title",question.getTitle());
        questionForm.put("content",content);
        questionForm.put("starttime",starttime);
        questionForm.put("finishtime",finishtime);
        return ServiceMessageBuilder
                .buildMessage(200,"success",questionForm);
    }
}
